/**
 * A thread corresponding an employee;a thread is used to perform a given task
 * 
 * @author zhongnanhai
 *
 */
public class EmployeeThread extends Thread{
	/**The running flag of this thread*/
	private boolean runningFlag = false;
	/**The given task*/
	private Task task = null;
	/**The employee bound to this thread*/
	private Employee employee = null;
	/**
	 * When creating an employee thread,bound an employee to it.
	 * 
	 * @param employee the bound employee
	 */
	public EmployeeThread(Employee employee){
		this.employee = employee;
	}
	/**
	 * The thread is running or not
	 * 
	 * @return the running flag
	 */
	public boolean isRunning(){
		return runningFlag;
	}
	/**
	 * Set the running flag of this thread.
	 * 
	 * @param flag the new running flag
	 */
	public synchronized void setRunning(boolean flag){
		runningFlag = flag;
		if(flag)
			this.notify();
	}
	/**
	 * Set the task that the corresponding employee will perform
	 * 
	 * @param task the given task
	 */
	public void setTask(Task task){
		this.task = task;
	}
	/**
	 * The run function of this thread. When performing a task,this thread should
	 * "using" a time.(by sleep() method)
	 */
	public synchronized void run(){
		try{
			while(true){
				if(!runningFlag){
					//System.out.println("employee "+employee.getemployeename()+" is waiting for a new task to perform...");
					this.wait();
				}
				else{
					int taskType = task.getTaskType();
					float productivity = employee.getProductivity().get(taskType);
					float time = (task.getSize()/productivity);//####$#$#$#$#$#^%$$$$$
					employee.setAvailable(false);
					task.setFinished(false);
					
					sleep((int)(time*10));
					//System.out.println("employee "+employee.getemployeename()+"has finished the task "+task.getName()+",using time "+(int)(time*100));
					
					task.setFinished(true);
					employee.setAvailable(true);
					
					setRunning(false);
				}
			}
		}catch(InterruptedException e){
			System.out.println("Interrupt");
		}
	}
}
